package studyNote;

public class BankAccount {
    private final String accountNumber;
    private double balance;
    private final String ownerName;

    // 构造器
    public BankAccount(String accountNumber, double initialBalance, String ownerName) {
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
        this.ownerName = ownerName;
    }

    // 方法1：转账到另一个账户（直接访问其他实例的私有字段）
    public boolean transferTo(BankAccount targetAccount, double amount) {
        if (this.balance >= amount) {
            this.balance -= amount;                   // 修改自己的余额
            targetAccount.balance += amount;          // 直接修改目标账户的私有余额
            System.out.println("转账成功: " + amount + " 从 " + this.ownerName + " 到 " + targetAccount.ownerName);
            return true;
        } else {
            System.out.println("余额不足，转账失败");
            return false;
        }
    }

    // 方法2：比较余额（直接访问其他实例的私有字段）
    public void compareBalanceWith(BankAccount otherAccount) {
        if (this.balance > otherAccount.balance) {
            System.out.println(this.ownerName + " 比 " + otherAccount.ownerName + " 更有钱");
        } else if (this.balance < otherAccount.balance) {
            System.out.println(otherAccount.ownerName + " 比 " + this.ownerName + " 更有钱");
        } else {
            System.out.println("两人余额相同");
        }
    }

    // 方法3：复制余额从另一个账户
    public void copyBalanceFrom(BankAccount sourceAccount) {
        this.balance = sourceAccount.balance;  // 直接访问源账户的私有余额
        System.out.println("已从 " + sourceAccount.ownerName + " 复制余额");
    }

    // 方法4：获取其他账户的信息（访问多个私有字段）
    public void printOtherAccountInfo(BankAccount otherAccount) {
        System.out.println("账户: " + otherAccount.accountNumber);
        System.out.println("户主: " + otherAccount.ownerName);
        System.out.println("余额: " + otherAccount.balance);
    }

    // 获取自身余额
    public double getBalance() {
        return balance;
    }

    // 获取户主名
    public String getOwnerName() {
        return ownerName;
    }

    // 主方法测试
    public static void main(String[] args) {
        // 创建两个银行账户
        BankAccount account1 = new BankAccount("001", 1000.0, "张三");
        BankAccount account2 = new BankAccount("002", 500.0, "李四");

        System.out.println("=== 初始状态 ===");
        System.out.println("张三余额: " + account1.getBalance());
        System.out.println("李四余额: " + account2.getBalance());

        System.out.println("\n=== 比较余额 ===");
        account1.compareBalanceWith(account2);  // 直接访问对方私有字段

        System.out.println("\n=== 转账操作 ===");
        Boolean result = account1.transferTo(account2, 300.0);   // 直接修改对方私有字段
        System.out.println(result);

        System.out.println("\n=== 转账后状态 ===");
        System.out.println("张三余额: " + account1.getBalance());
        System.out.println("李四余额: " + account2.getBalance());

        System.out.println("\n=== 复制余额 ===");
        BankAccount account3 = new BankAccount("003", 0.0, "王五");
        account3.copyBalanceFrom(account2);     // 直接访问对方私有字段
        System.out.println("王五余额: " + account3.getBalance());

        System.out.println("\n=== 查看他人账户信息 ===");
        account1.printOtherAccountInfo(account2);  // 访问多个私有字段
    }
}
